package pl.polsl.mmm.projectcomm.input.button;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
//import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import java.util.HashMap;

/**
 * Klasa reprezentuje przycisk wyświetlany na ekranie gry
 *
 * Created by michal on 29.11.14.
 */
public class Button extends View {

    private Paint paintRed;
    private Paint paintGreen;

    private ButtonClickedListener clickListener;

    // Środek widoku
    private int cX, cY;

    // Promień
    private int radius;

    // Rozmiar widoku
    private int dimX, dimY;

    // Aktualny stan
    private boolean state = false;

    // Tekstura przycisku normalnego i wciśniętego
    private Bitmap normalBitmap;
    private Bitmap pressedBitmap;

    public Button(Context context) {
        super(context);
        initButtonView();
    }

    public Button(Context context, AttributeSet attrs) {
        super(context, attrs);
        initButtonView();
    }

    public Button(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initButtonView();
    }

    public void setNormalBitmap(Bitmap newBitmap) {
        this.normalBitmap = newBitmap;
    }

    public void setPressedBitmap(Bitmap newBitmap) {
        this.pressedBitmap = newBitmap;
    }

    private void initButtonView() {
        setFocusable(true);


        paintRed = new Paint(Paint.ANTI_ALIAS_FLAG);
        paintRed.setColor(Color.RED);
        paintRed.setStrokeWidth(1);
        paintRed.setStyle(Paint.Style.FILL_AND_STROKE);

        paintGreen = new Paint(Paint.ANTI_ALIAS_FLAG);
        paintGreen.setColor(Color.GREEN);
        paintGreen.setStrokeWidth(1);
        paintGreen.setStyle(Paint.Style.FILL_AND_STROKE);

    }

    // Zmienia stan przycisku na podany
    public void setState(boolean newState) {
        this.state = newState;
        this.invalidate();
    }

    // Zwraca stan przycisku
    public boolean getState() {
        return this.state;
    }

    // Zmienia stan przycisku na przeciwny
    public boolean changeState() {
        this.state = !this.state;
        this.invalidate();
        return this.state;
    }

    // =========================================
    // Rysowanie
    // =========================================

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // Here we make sure that we have a perfect circle
        int measuredWidth = measure(widthMeasureSpec);
        int measuredHeight = measure(heightMeasureSpec);
        setMeasuredDimension(measuredWidth, measuredHeight);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        int d = Math.min(getMeasuredWidth(), getMeasuredHeight());


        cX = d / 2;
        cY = d / 2;

        radius = ( d - 20 ) / 2;

    }

    private int measure(int measureSpec) {
        int result = 0;
        // Decode the measurement specifications.
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        if (specMode == MeasureSpec.UNSPECIFIED) {
            // Return a default size of 200 if no bounds are specified.
            result = 200;
        } else {
            // As you want to fill the available space
            // always return the full available bounds.
            result = specSize;
        }
        return result;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.save();

        Paint paint;

        if (this.state) {
            if (this.pressedBitmap != null) {
                canvas.drawBitmap(this.pressedBitmap, new Rect(0, 0, 124, 124), new Rect(0, 0, 60, 60), null);
            } else {
                // jeśli tekstura niedostępna - rysuj zielone kółko
                paint = this.paintGreen;
                canvas.drawCircle(cX, cY, radius, paint);
            }
        } else  {
            if (this.normalBitmap != null) {
                canvas.drawBitmap(this.normalBitmap, new Rect(0, 0, 124, 124), new Rect(0, 0, 60, 60), null);
            } else {
                // jeśli tekstura niedostępna - rysuj czerwone kółko
                paint = this.paintRed;
                canvas.drawCircle(cX, cY, radius, paint);
            }
        }

        canvas.restore();
    }

    // Obsługa dotyku
    @Override
    public boolean onTouchEvent(MotionEvent ev) {


        final int action = ev.getAction();

        switch (action & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN: {
                this.changeState();
                this.clickListener.OnClicked();
                break;
            }
        }
        return false;
    }

    public void setOnButtonClickedListener(ButtonClickedListener listener) {
        this.clickListener = listener;
    }

}
